<input type="hidden" value="<?php echo $subject_delete;?>" id="subject_id_delete" name="subject_id_delete" />
<input type="hidden" value="<?php echo $semester_delete;?>" id="semester_id_delete" name="semester_id_delete" />
<table class="education-form" id="legend-box-5">
	<tr valign="top">
		<td align="left" style="vertical-align: top;padding-left: 0px;"><strong>Thành tích học tập *</strong></td>
		<td>
		<div class="legendColorBox">
			<div style="background: #8ec657" id="semester-add" class="legendColor add-semester">Thêm</div>
		</div>
		</td>
	</tr>
	<tr id="education-detail" valign="top" <?php echo ((empty($semesters)) ? 'style="display: none"' : null);?>">
		<td>&nbsp;</td>
		<td class="border-bottom border-right border-left border-top">
			<table id="semester-table">
			<?php if (!empty($semesters)) :?>
				<?php foreach ($semesters as $sem => $semester):?>
					<tr id="semester-tr-<?php echo $sem;?>" class="semester" valign="top">
						<td class="semester-label border-bottom" id="semester-td-title-<?php echo $sem;?>">
							<span <?php echo (isset($semester['id'])) ? 'id = "' . $semester['id'] . '"' : '';?> class="delete-semester collapse-toggle-delete">&nbsp;&nbsp;</span>
							<input type="text" value="<?php echo $semester['name']?>" name="Semester[<?php echo $sem;?>][name]">
							<?php if (isset($semester['id'])):?>
							<input type="hidden" name="Semester[<?php echo $sem;?>][id]" value="<?php echo $semester['id']?>">
							<?php endif;?>
						</td>
						<td class="border-bottom" id="semester-td-detail-<?php echo $sem;?>">
							<?php if (isset($subjects[$sem]) && !empty($subjects[$sem])) :?>
							<table cellpadding="2" class="table-subject" id="subject-table-<?php echo $sem;?>">
								<tbody>
									<?php foreach ($subjects[$sem] as $sub => $subject):?>
										<?php $trSubject 	= '<tr class = "subject-tr-' . $sem . '" id="subject-tr-' . $sem . '-' . $sub . '">';?>
										<?php $trSubject 	.= '<td class="subject-label"><input type="text" value="'.$subject['name'].'" name="Subject[' . $sem . '][' . $sub . '][name]">'?>
										<?php $trSubject 	.= ((isset($subject['id'])) ? ('<input type="hidden" name="Subject[' . $sem . '][' . $sub . '][id]" value="'.$subject['id'].'" >') : '') . '</td>'?>
										<?php $trSubject 	.= '<td class="subject-value"><input type="text" value="'.$subject['score'].'" name="Subject[' . $sem . '][' . $sub . '][score]"><span ' . ((isset($subject['id'])) ? 'id = "' . $subject['id'] . '"' : '') . ' class="delete-subject collapse-toggle-delete">&nbsp;&nbsp;</span></td>'?>
										<?php echo $trSubject 	.= '</tr>';?>
									<?php endforeach;?>
									<tr id="subject-tr-<?php echo $sem;?>-summary">
										<td class="subject-label">
											<input type="text" class="summary" value="<?php echo ($semester['summary_label'] == '') ? 'Tổng kết TB' : $semester['summary_label'];?>" name="Semester[<?php echo $sem;?>][summary_label]" id="summary-label-<?php echo $sem;?>">
										</td>
										<td class="subject-value">
											<input type="text" value="<?php echo $semester['summary_value'];?>" name="Semester[<?php echo $sem;?>][summary_value]" id="summary-value-<?php echo $sem;?>">
											<span id = "subject-tr-<?php echo $sem;?>" class="subject-add collapse-toggle-add">&nbsp;&nbsp;</span>
										</td>
									</tr>
								</tbody>
							</table>
							<?php endif;?>
						</td>
					</tr>
				<?php endforeach;?>
			<?php endif?>
			</table>
		</td>
	</tr>
</table>
<script type="text/javascript">
	$(".delete-semester").live("click", function(event){
		var answer = confirm("Bạn muốn xóa dữ liệu này ?");
	    if (answer){
	    	if (this.id != '') {
				var id_delete 	= $("#semester_id_delete").val();
				id_delele		= id_delete + ((id_delete != '') ? ',' : '') + this.id;
				$("#semester_id_delete").val(id_delele);
			}

	    	$(this).closest("tr").remove();
	    }
	    return false;
	});
	$(".delete-subject").live("click", function(event){
		var answer = confirm("Bạn muốn xóa dữ liệu này ?");
	    if (answer){
	    	if (this.id != '') {
				var id_delete 	= $("#subject_id_delete").val();
				id_delele		= id_delete + ((id_delete != '') ? ',' : '') + this.id;
				$("#subject_id_delete").val(id_delele);
			}

	    	$(this).closest("tr").remove();
	    }
	    return false;
	});
	$(".subject-add").live("click", function(event){
		var index_subject 	= 0;
		var index_semester	= this.id.replace('subject-tr-', '');
		var class_summary	= this.id + '-summary';
		if ($('.' + this.id).length > 0) {
			var last_subject_id = $('.' + this.id).last().attr('id');
			var substr 			= last_subject_id.split('-');
			index_subject		= parseInt(substr[3]) + 1;
		}
		// append detail semester
		$('#' + class_summary).before('<tr class = "subject-tr-' + index_semester + '" id = "subject-tr-' + index_semester + '-' + index_subject + '"></tr>');

		// append subject
		$('#subject-tr-' + index_semester + '-' + index_subject).append('<td class = "subject-label"><input type="text" name="Subject[' + index_semester + '][' + index_subject + '][name]" value="Môn học"></td>');
		$('#subject-tr-' + index_semester + '-' + index_subject).append('<td class = "subject-value"><input type="text" name="Subject[' + index_semester + '][' + index_subject + '][score]" value="Điểm"><span class = "delete-subject collapse-toggle-delete">&nbsp;&nbsp;</span></td>');

	    return false;
	});
//alert($.fn.jquery);
	$("#semester-add").click(function(){
		var count_semester 	= $(".semester").length;
		var index_semester	= 1;
		if (count_semester == 0) {
			$("#education-detail").show();
		}
		else {
			index_semester 	= parseInt($(".semester").last().attr('id').replace('semester-tr-', '')) + 1;
		}
		addSemester(index_semester);
	});
	function addSemester(index) {
		// append row semester
		$("#semester-table").append('<tr id="semester-tr-' + index + '" class="semester" valign="top"></tr>');

		// append col semester title and detail
		$("#semester-tr-" + index).append('<td id="semester-td-title-' + index + '" class="semester-label border-bottom"></td>');
		$("#semester-tr-" + index).append('<td id="semester-td-detail-' + index + '" class="border-bottom"></td>');

		// append title semester
		$("#semester-td-title-" + index).append('<span class="delete-semester collapse-toggle-delete">&nbsp;&nbsp;</span>');
		$("#semester-td-title-" + index).append('<input type="text" id="semester-title-1-'+ index + '" name="Semester[' + index + '][name]" value="Học kì">');

		// append detail semester
		$("#semester-td-detail-" + index).append('<table id = "subject-table-' + index + '" class= "table-subject" cellpadding="2"></table>');
		$("#subject-table-" + index).append('<tr class = "subject-tr-' + index + '" id = "subject-tr-' + index + '-0"></tr><tr id = "subject-tr-' + index + '-summary"></tr>');

		// append subject
		$("#subject-tr-" + index + "-0").append('<td class = "subject-label"><input type="text" name="Subject[' + index + '][0][name]" value="Môn học"></td>');
		$("#subject-tr-" + index + "-0").append('<td class = "subject-value"><input type="text" name="Subject[' + index + '][0][score]" value="Điểm"><span class = "delete-subject collapse-toggle-delete">&nbsp;&nbsp;</span></td>');

		// append subject summary
		$("#subject-tr-" + index + "-summary").append('<td class = "subject-label"><input type="text" id="summary-label-' + index + '" name="Semester[' + index + '][summary_label]" value="Tổng kết TB" class="summary"></td>');
		$("#subject-tr-" + index + "-summary").append('<td class = "subject-value"><input type="text" id="summary-value-' + index + '" name="Semester[' + index + '][summary_value]" value="Điểm"><span id = "subject-tr-' + index + '" class="subject-add collapse-toggle-add">&nbsp;&nbsp;</span></td>');

	}
</script>
